package com.caszx.chargingpile.activity;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONException;
import com.app.base.adapter.BaseClickRecyclerAdapter;
import com.app.base.adapter.BaseHeadFootAdapter;
import com.app.base.app.BaseTopBarNetActivity;
import com.app.jsonparse.JSONParse;
import com.app.utils.DiskUser;
import com.caszx.chargingpile.R;
import com.caszx.chargingpile.adapter.LocationHeadAdapter;
import com.caszx.chargingpile.entity.EntityCity;
import com.caszx.chargingpile.holder.station.CityHeadHolder;
import com.caszx.chargingpile.holder.station.CityHolder;
import com.caszx.chargingpile.http.ConstantUrl;
import com.caszx.chargingpile.user.LocationManager;
import com.eowise.recyclerview.stickyheaders.StickyHeadersBuilder;
import com.eowise.recyclerview.stickyheaders.StickyHeadersItemDecoration;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;
import http.okhttp.OkHttpUtils;
import http.okhttp.callback.JsonCallback;
import okhttp3.Call;

public class CityActivity extends BaseTopBarNetActivity {


    private RecyclerView mRecyclerView;
    private BaseHeadFootAdapter<EntityCity> mRecyclerAdapter;
    private LocationHeadAdapter mLocationHeadAdapter;
    private StickyHeadersItemDecoration top;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCenterTitle("城市");
    }

    @Override
    protected void onLoad() {
        HashMap<String,String> params = new HashMap<>();
        params.put("token", DiskUser.getStringValueByKey(this,"token"));
        OkHttpUtils.post()
                .url(ConstantUrl.URL_CITY_STATION)
                .params(params)
                .tag(this)
                .build()
                .execute(new JsonCallback() {
                    @Override
                    public void onError(Call call, Exception e) {
                        showErrorView(R.string.error_network);
                    }

                    @Override
                    public void onResponse(JSONObject response) {
                        JSONObject resultJsonObject =  response.optJSONObject("result");
                        if(resultJsonObject!=null){
                            int code =  resultJsonObject.optInt("code");
                            if(code<0){
                                //error
                                // DialogHelp.showDialogOnlyMesg(resultJsonObject.optString("msg"),null);
                                showErrorView(resultJsonObject.optString("msg"));
                            }else{
                                try {
                                    showContentView();
                                    List<EntityCity> cityList = JSONParse.parseArray(response.optString("data"),EntityCity.class);
                                    mRecyclerView.removeItemDecoration(top);
                                    if(mLocationHeadAdapter == null){
                                        mLocationHeadAdapter = new LocationHeadAdapter(mRecyclerAdapter.getItems());
                                        top = new StickyHeadersBuilder()
                                                .setAdapter(mRecyclerAdapter)
                                                .setRecyclerView(mRecyclerView)
                                                .setStickyHeadersAdapter(mLocationHeadAdapter)
                                                // .setOnHeaderClickListener(LocalPriorityFragment.this)
                                                .build();
                                    }
                                    mRecyclerView.addItemDecoration(top);
                                    mRecyclerAdapter.setDatas(cityList);
                                    mLocationHeadAdapter.setData(cityList);
                                }catch (JSONException e){
                                    showErrorView(R.string.error_parser_json);
                                }
                            }
                        }
                    }
                });
    }

    @Override
    protected View initOnloadedView() {
        View view = getLayoutInflater().inflate(R.layout.common_recyclerview,null);
        initRecyclerView(view);
        return view;
    }

    private void initRecyclerView(View view) {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerAdapter = new BaseHeadFootAdapter<>(R.layout.item_city, CityHolder.class);
        mRecyclerAdapter.setHeadViewId(R.layout.item_city, CityHeadHolder.class);
        mRecyclerAdapter.setHeaderData(LocationManager.getInstance().getUserCityName());
        mRecyclerAdapter.setOnItemClickListener(new BaseClickRecyclerAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if(position == 0){
                    EntityCity city = new EntityCity();
                    city.city = LocationManager.getInstance().getUserCityName();
                    city.latitude = LocationManager.getInstance().getUserLatitude();
                    city.longitude = LocationManager.getInstance().getUserLongitude();
                    EventBus.getDefault().post(city);
                }
                if(position !=0){
                    EventBus.getDefault().post(mRecyclerAdapter.getItems().get(position-1));
                }
                finish();

            }
        });
        mRecyclerView.setAdapter(mRecyclerAdapter);
    }
}
